﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TwitterLikeWebChatDAL
{
    public static class TwitterLikeWebChatDAO
    {
        public static string[] GetChannelNames()
        {
            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                return context.Channels.Select(ch => ch.ChannelName).ToArray();
            }
        
        }

        public static int GetChannelIdByChannelName(string channelName)
        {
            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                 return context.Channels.First(ch => ch.ChannelName == channelName).ChannelId;
            }
        }

        public static Message[] GetMessagesByChannelName(string channelName, int maxMessagesCount = int.MaxValue)
        {
            int channelId = GetChannelIdByChannelName(channelName);

            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                IQueryable<Message> messages = context.Messages.Where(m => m.ChannelId == channelId);

                IQueryable<Message> topNMessages = messages.OrderByDescending(m => m.PostDateTime).Take(maxMessagesCount);
                IQueryable<Message> topNMessageConents = topNMessages.OrderBy(m => m.PostDateTime);
                return topNMessageConents.ToArray();
            }
        }
        
        public static void DeleteAllMessagesByChannelName(string channelName)
        {
            int channelId = GetChannelIdByChannelName(channelName);

            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                IQueryable<Message> toBeDeleted = context.Messages.Where(m => m.ChannelId == channelId);

                foreach (var messageToDelete in toBeDeleted)
                {
                    context.Messages.DeleteObject(messageToDelete);
                }

                context.SaveChanges();
            }
        }

        public static void UploadMessageByChannelName(string channelName, string message)
        {
            int channelId = GetChannelIdByChannelName(channelName);

            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                Message newMessage = new Message();
                newMessage.ChannelId = channelId;
                newMessage.MessageContent = message;
                newMessage.PostDateTime = DateTime.Now;

                context.Messages.AddObject(newMessage);
                context.SaveChanges();
            }
        }

        public static void CreateNewChannel(string channelName)
        {
            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                Channel channel = new Channel();
                channel.ChannelName = channelName;

                context.Channels.AddObject(channel);

                context.SaveChanges();
            }
        }

        public static void DeleteChannel(string channelName)
        {
            using (TwitterLikeWebChatEntities context = new TwitterLikeWebChatEntities())
            {
                Channel channel = context.Channels.FirstOrDefault(ch => ch.ChannelName == channelName);

                if (channel != null)
                {
                    foreach (Message m in context.Messages.Where(m => m.ChannelId == channel.ChannelId))
                    {
                        context.Messages.DeleteObject(m);
                    }

                    context.Channels.DeleteObject(channel);
                    context.SaveChanges();
                }
            }
        }
    }
}
